<?PHP
session_start();
include("common/connect.php");
include("common/common.php");

if( !isset($_SESSION["user"]) ) 
{
	$url = "Location:login.php?ref=view_trade.php";
	header($url);
}

$opt = CheckUrlParamNoDie("opt");
$tradeid = CheckUrlParamNoDie("tradeid");
$tradeid_begin = substr($tradeid, 0, 6);
$tradeid_end = substr($tradeid, -4);

$id = "";
$tbid = trim(GetPostParamStr("tb"));
$phone = trim(GetPostParamStr("phone"));
$info = trim(GetPostParamStr("info"));

if ($tradeid == "")
{
	echo "error!! call zhangjunbin!";
	exit;
}

//echo $opt;
if ($opt == "getphone")
{
	$tbid = trim(GetPostParamStr("tb"));

	// 取Phone
	if ($tbid)
	{
		$result = mysql_query( "select phone from user where tb_id='" .$tbid. "' limit 1" );
		if ($result)
		{
			$row = mysql_fetch_assoc( $result );
			$phone = $row["phone"];
		}
	}
}

if ($opt == "modify12")
{
	mysql_query( "update report_items set handle_code=0 where trade_id like '".$tradeid_begin."%".$tradeid_end."'");
//	echo "update report_items set handle_code=0 where trade_id like '".$tradeid_begin."%".$tradeid_end;
	if (mysql_affected_rows() == 0)
		echo "<script>alert('出错了，请找程序员。');</script>";
	else
		echo "<script>window.history.go(-2);</script>";

	exit;
}


	
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<style type="text/css">
<!--
body,td,th {
	font-family: 宋体;
	font-size: 13px;
	color: #000;
}
body {
	background-color: #FFF;
}
-->
</style>

<script language="javascript" src="js/common.js"></script>
<script language="javascript">
function OnBtn(strAction)
{
	var tbid = document.getElementById("tb_id").value;
	var phone = document.getElementById("phone_id").value;
	
	if( tbid == "" || ((!isemail( tbid )) && (!isphone( tbid ))) )
	{
		alert( "用户支付宝帐号输入有误！" );
		document.getElementById("tb_id").focus();
		return;
	}
	
	document.getElementById("form1").action = strAction + "&id=<?PHP echo $id; ?>&tbid=<?PHP echo $tbid; ?>&tradeid=<?PHP echo $tradeid; ?>";
	document.getElementById("form1").submit();
}
</script>

<?php

//echo $t[1];
//exit;
if ($opt == "savein" || $opt == "savein_ok")
{
	$bInOK = 0;
	if ($opt == "savein_ok")
		$bInOK = 1;
	
//	echo GetPostParamStr("desIds")." ".$tbid."  ".$tradeid." ".$phone;
//	exit;
//	if (0){
	$desIds = explode(',', GetPostParamStr("desIds"));
	for($i=0; $i < count($desIds) - 1; $i++)	// -1去掉最后的空项
	{
		_DealItemSrc($bInOK, $desIds[$i], $tbid, $phone, $info);
	}
//	}
	
	echo "<script>alert('处理成功！');window.history.go(-3);</script>";
//	echo "<script>alert('处理成功！');/script>";
	exit;
}

function _DealItemSrc( $bInOK, $id, $tbid, $phone, $info)
{
	echo "update order_handle_report set tbid='$tbid',phone='$phone',info='$info',operater='".$_SESSION["user"]."',handle_code=$bInOK where id=$id limit 1";
	//return;
	$result = mysql_query( "update order_handle_report set tbid='$tbid',phone='$phone',info='$info',operater='".$_SESSION["user"]."',handle_code=$bInOK where id=$id limit 1" );
	if (mysql_affected_rows() == 0)
	{
		echo "<script>alert('处理过程中发生失败，请告诉程序员，出错代码：1');window.history.back();</script>";
		exit;
	}
}

?>
</head>

<body>
<br />

<table cellpadding="0" cellspacing="0" border="1" align="center">
	<tr>
    	<td height="25" width="50" align="center">序列号</td>
        <td width="200" align="center">时间</td>
        <td width="158" align="center">支付宝帐号</td>
      	<td width="198" align="center">订单号</td>
        <td width="198" align="center">商品Id</td>
        <td width="70" 	align="center">交易额</td>
        <td width="144" align="center">佣金额</td>
        <td width="143" align="center">操作人</td>
        <td width="143" align="center">操作</td>
    </tr>

<?PHP
$fNeedBackMoney = 0.0;
$desIds = "";	// 目标表的Id
$strTradeIds = "购物兔返款，如果是促销产品，实际返现会稍少一些，本次返款订单列表如下：";	// 本次交易订单号列表

$sql = "select * from order_handle_report where trade_id like '".$tradeid_begin."%".$tradeid_end."' order by opt_time desc";
echo $sql;
//exit;
$lastTbId = "";
$result = mysql_query( $sql );
while( $result && $row = mysql_fetch_assoc( $result ))
{
	if ($lastTbId != "" and $lastTbId != $row["tbid"])
	{
		echo "<tr><td>出错啦！！！！请找冰峰</td></tr>";
		break;
	}
	else
	{
		$lastTbId = $row["tbid"];
		if ($opt == "")
		{	
			$tbid = $row["tbid"];
			$phone = $row["phone"];
			$info = $row["info"];
		}
	}
		
	$fNeedBackMoney += $row["commission"];
	$desIds .= $row["id"] . ",";
	$strTradeIds .= $row["trade_id"].", ￥".round($row["commission"]*0.6, 2)."; ";
?>
<tr>
    <td height="25" width="50" align="center"><?PHP echo $row["id"]; ?></td>
    <td width="200" align="center" title="<?PHP echo "操作时间：".$row["opt_time"]; ?>"><?PHP echo $row["pay_time"]; ?></td>
	<td width="198" align="center" title="<?PHP echo $phone; ?>"><?PHP echo $lastTbId; ?>&nbsp;</td>
	<td width="70" 	align="center" title="<?PHP echo $row["item_num"]."件"; ?>"><?PHP echo $row["trade_id"]; ?>&nbsp;</td>
    <td width="70" 	align="center" title="<?PHP echo $row["item_title"]; ?>"><?PHP echo $row["num_iid"]; ?>&nbsp;</td>
	<td width="144" align="center" title="<?PHP echo $row["real_pay_fee"]; ?>"><?PHP echo $row["pay_price"]; ?>&nbsp;</td>
	<td width="158" align="center" title="<?PHP echo ($row["commission_rate"] * 100)."%"; ?>"><?PHP echo $row["commission"]; ?>&nbsp;</td>
    <td align="center"><?PHP echo $row["operater"]; ?>&nbsp;</td>
	<td width="143" align="center"><?PHP if ($row["handle_code"] == 0) echo "未返"; else echo "已返"; ?>&nbsp;</td>
</tr>
<?PHP
}
?>

</table>


<hr />
<form method="post" id="form1" name="form1" >
<table cellpadding="0" cellspacing="0" border="1" align="center">
    <tr>
   	  <td width="60" height="30" align="center">账号：</td>
      <td width="140" height="22"><input type="text" width="140" id="tb_id" name="tb" height="20" value="<?PHP echo $tbid; ?>" onclick="javascript:select();" /></td>
      <td height="30"> 手机号： 
        <input type="text" width="140" id="phone_id" name="phone" height="20" value="<?PHP echo $phone; ?>" onclick="javascript:select();" /><input type="button" name="btnin" value="更新数据" onclick="javascript:OnBtn('view_trade.php?opt=getphone');" />
        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
        <?PHP $_data = ($tbid."|".$phone."|".(round($fNeedBackMoney * 0.6, 2))."|购物兔返款，欢迎推荐给好友~|".$strTradeIds); ?>
<a href="http://_my_data_transport_/?<?PHP echo $_data; ?>" target="_blank">去返款</a>
</td>
    </tr>
    <tr>
    	<td colspan="2" height="30" align="right">共需返还金额：</td>
        <td><input type="text"  width="340" id="all_return_id" name="all_return" height="20" value="<?PHP echo round($fNeedBackMoney * 0.6, 2); ?>" />&nbsp;</td>
    </tr>
    <tr>
    	<td colspan="2" height="30" align="right"s>备注：</td>
        <td><textarea name="info" cols="56" rows="7"><?PHP echo $info; ?></textarea></td>
    </tr>
    <tr>
    	<td colspan="3" height="30" align="center">
		<input type="hidden" name="desIds" value="<?PHP echo $desIds; ?>" />
        <input type="button" name="btnin" value="修改入库" onclick="javascript:OnBtn('view_trade.php?opt=savein');"/>&nbsp;&nbsp;
        <input type="button" name="btnin_ok" value="修改入库并标志返现" onclick="javascript:OnBtn('view_trade.php?opt=savein_ok');"/>
        </td>
    </tr>
</table>
</form>

<div align="center">
<input type="text" width="180" height="20" value="购物兔返款，欢迎推荐给好友" onclick="javascript:select();" />
<input type="text" width="180" height="20" value="<?PHP echo $strTradeIds; ?>" onclick="javascript:select();" />
<?PHP 
$hasReturn = mysql_result(mysql_query("select sum(commission)*0.6 as tmp from order_handle_report where tbid='".$tbid."'"), 0, "tmp");
echo "此用户共返了 ".$hasReturn."元";
?>
</div>

</body>
</html>